package com.farben.web.controller.aging;

import java.util.List;

import com.farben.common.core.domain.entity.SysUser;
import com.farben.common.core.domain.model.LoginUser;
import com.farben.common.utils.ServletUtils;
import com.farben.framework.web.service.TokenService;
import com.farben.system.domain.AgingDesignUserAppLink;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.farben.common.annotation.Log;
import com.farben.common.core.controller.BaseController;
import com.farben.common.core.domain.AjaxResult;
import com.farben.common.enums.BusinessType;
import com.farben.system.domain.AgingDesignAppInfo;
import com.farben.system.service.IAgingDesignAppInfoService;
import com.farben.common.utils.poi.ExcelUtil;
import com.farben.common.core.page.TableDataInfo;

/**
 * 后管应用信息Controller
 *
 * @author farben
 * @date 2023-05-16
 */
@Api("适老化应用信息")
@RestController
@RequestMapping("/system/aging_design/appInfo")
public class AgingDesignAppInfoController extends BaseController {
    @Autowired
    private IAgingDesignAppInfoService agingDesignAppInfoService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询后管应用信息列表
     */
    @ApiOperation("查询后管应用信息列表")
    @PreAuthorize("@ss.hasPermi('system:appInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(AgingDesignAppInfo agingDesignAppInfo) {
        startPage();

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        agingDesignAppInfo.setUserId(user.getUserName());

        List<AgingDesignAppInfo> list = agingDesignAppInfoService.selectAgingDesignAppInfoList(agingDesignAppInfo);
        return getDataTable(list);
    }

    /**
     * 导出后管应用信息列表
     */
    @ApiOperation("导出后管应用信息列表")
    @ApiImplicitParam(name = "AgingDesignAppInfo", value = "应用信息", dataType = "AgingDesignAppInfo")
    @PreAuthorize("@ss.hasPermi('system:appInfo:export')")
    @Log(title = "后管应用信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AgingDesignAppInfo agingDesignAppInfo) {
        List<AgingDesignAppInfo> list = agingDesignAppInfoService.selectAgingDesignAppInfoList(agingDesignAppInfo);
        ExcelUtil<AgingDesignAppInfo> util = new ExcelUtil<AgingDesignAppInfo>(AgingDesignAppInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 获取后管应用信息详细信息
     */
    @ApiOperation("获取后管应用信息详细信息")
    @ApiImplicitParam(name = "id", value = "appId", required = true, dataType = "long", paramType = "path")
    @PreAuthorize("@ss.hasPermi('system:appInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(agingDesignAppInfoService.selectAgingDesignAppInfoById(id));
    }

    /**
     * 新增后管应用信息
     */
    @ApiOperation("新增后管应用信息")
    @ApiImplicitParam(name = "AgingDesignAppInfo", value = "应用信息", dataType = "AgingDesignAppInfo")
    @PreAuthorize("@ss.hasPermi('system:appInfo:add')")
    @Log(title = "后管应用信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AgingDesignAppInfo agingDesignAppInfo) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();

        int rows = agingDesignAppInfoService.insertAgingDesignAppInfo(agingDesignAppInfo);
        AgingDesignUserAppLink userAppLink = AgingDesignUserAppLink.builder()
                .appId(agingDesignAppInfo.getAppId())
                .userId(user.getUserName())
                .createdBy(user.getUserName())
                .createdDate(agingDesignAppInfo.getCreatedDate())
                .updateBy(user.getUserName())
                .updateDate(agingDesignAppInfo.getUpdatedDate())
                .build();
        if (rows == 0) {
            return toAjax(rows);
        }
        int result = agingDesignAppInfoService.insertAgingDesignUserAppLink(userAppLink);
        return toAjax(result);
    }

    /**
     * 更新后管应用信息
     */
    @ApiOperation("更新后管应用信息")
    @ApiImplicitParam(name = "AgingDesignAppInfo", value = "应用信息", dataType = "AgingDesignAppInfo")
    @PreAuthorize("@ss.hasPermi('system:appInfo:edit')")
    @Log(title = "后管应用信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AgingDesignAppInfo agingDesignAppInfo) {
        return toAjax(agingDesignAppInfoService.updateAgingDesignAppInfo(agingDesignAppInfo));
    }

    /**
     * 删除后管应用信息
     */
    @ApiOperation("删除后管应用信息")
    @ApiImplicitParam(name = "ids", value = "主键Ids", required = true, dataType = "long", paramType = "path")
    @PreAuthorize("@ss.hasPermi('system:appInfo:remove')")
    @Log(title = "后管应用信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(agingDesignAppInfoService.deleteAgingDesignAppInfoByIds(ids));
    }
}
